package cn.xuexiyuan.kpi.controller;

import cn.xuexiyuan.kpi.entity.ResponseEntity;
import cn.xuexiyuan.kpi.entity.UserEntity;
import cn.xuexiyuan.kpi.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/register")
    public ResponseEntity register(@RequestBody UserEntity user) {
        if(null != userMapper.findOneByName(user.getUser_name())){
            return ResponseEntity.buildFail("用户已存在");
        }


        userMapper.insert(user);
        return ResponseEntity.buildSucess("注册成功");
    }

    @RequestMapping("/login")
    public ResponseEntity login(@RequestBody UserEntity user) {
        UserEntity res = userMapper.findOne(user);
        if(res != null){
            return ResponseEntity.buildSucess(res);
        }else{
            return ResponseEntity.buildFail("用户名或密码有误");
        }
    }

}
